import React, { Component } from 'react';
import {
	StyleSheet, Text, View, Image, TouchableOpacity, ScrollView, PixelRatio
} from "react-native";
import { GLOBAL } from '../../../../config/global';
import { style } from '../../../../theme/style';
import { Common } from '../../../../component/common/Common';
import { orderStyle, commonStyle, commen } from '../_common/style';
import { ModalCpt } from '../_components/ModalCpt';
import OrderDetailsService from '../_common/service';

const scale = GLOBAL.SCALE;

export class RktzDetailsPage extends Component {
	static navigationOptions = ({ navigation }) => {
		console.log(navigation.state.params, '0000')
		let { orderType } = navigation.state.params || {}
		return {
			headerTitle: (orderType == 'P' ? '采购' : '销售') + '订单-' + (orderType == 'P' ? '入库' : '出库') + '明细',
			headerTintColor: '#272727',
			headerLeft: <Common type="headLeft" onPress={() => { navigation.goBack() }} color='#272727' />,
			headerRight: (<Image style={{ marginLeft: 18 * scale, width: 9 * scale, height: 16 * scale }} source={require('../../../../imgs/goBackEmpty.png')} />),
			headerTitleStyle: {
				flex: 1,
				textAlign: 'center'
			},
			headerStyle: {
				elevation: 0,
				backgroundColor: '#F5F5F5',
				borderBottomWidth: 0,
			},
		}
	};

	constructor(props) {
		super(props);
		this.sendData = this.props.navigation.state.params || {};
		console.log('this.sendData', this.sendData)
		this.state = {
			isVisible: false
		}
	}

	async	componentWillMount () {
		this._init();
	}

	componentWillUnmount () {

	}

	//初始调用
	_init = () => {
		this._getData();
	}

	//获取详情数据
	_getData = async () => {
		let rsData = await OrderDetailsService.stockRk(this.sendData);
		console.log('入库/出库详情数据', rsData);
		if (rsData) {
			this.setState({ data: rsData })
		} else {
			this.setState({ status: 'empty' })
		}
	}

	//_modalCallBack
	_modalCallBack = (data) => {
		this.setState({
			isVisible: data.isVisible
		})
	}


	render () {
		let { isVisible, data } = this.state;
		let { orderType } = this.sendData;
		let { noticeId, statusName, remark, freeDays, originator, settlementParty, isDirectSellingName,
			deliveryTypeName, noticeItems,
			receivingAddress, shipper, shipperPhone, receivingWarehouseName,
			shipmentsAddress, consigner, consignerPhone, shipmentsWarehouseName } = data || {};
		return (
			<View style={styles.container}>
				<ScrollView style={{ flex: 1 }}>
					<View style={styles.itemBox}>
						<View style={[styles.itemBody]}>
							<View style={styles.detailsHead}>
								<View style={[commonStyle.row2, styles.headPd]}>
									<Text style={styles.detailsHeadTitle}>{orderType == 'P' ? '入库' : '出库'}单 {noticeId || '-'}</Text>
									<Text style={styles.detailsHeadStatus}>{statusName || '-'}</Text>
								</View>
								<View style={[commonStyle.row2, styles.topp]}>
									<View style={[commonStyle.row3, { flex: 1 }]}>
										<Text style={orderStyle.lable}>提货费发生方</Text>
										<Text style={orderStyle.value}>{settlementParty || '-'}</Text>
									</View>
									<View style={[commonStyle.row3, { flex: 1 }]}>
										<Text style={orderStyle.lable}>制单人</Text>
										<Text style={orderStyle.value}>{originator || '-'}</Text>
									</View>
								</View>
								<View style={[commonStyle.row2, styles.topp]}>
									<View style={[commonStyle.row3, { flex: 1 }]}>
										<Text style={orderStyle.lable}>是否直销</Text>
										<Text style={orderStyle.value}>{isDirectSellingName || '-'}</Text>
									</View>
									<View style={[commonStyle.row3, { flex: 1 }]}>
										<Text style={orderStyle.lable}>免仓期</Text>
										<Text style={orderStyle.value}>{freeDays || '-'}</Text>
									</View>
								</View>
								<View style={[commonStyle.row2, styles.topp]}>
									<View style={[commonStyle.row3, { flex: 1 }]}>
										<Text style={orderStyle.lable}>交货方式</Text>
										<Text style={orderStyle.value}>{deliveryTypeName || '-'}</Text>
									</View>
									<View style={[commonStyle.row3, { flex: 1 }]}>
										<Text onPress={() => { this.setState({ isVisible: true }) }} style={[orderStyle.loadColor, { paddingLeft: 40 * scale }]}>[收发货信息]</Text>
									</View>
								</View>
								<View style={[commonStyle.row2, styles.topp]}>
									<View style={[commonStyle.row3, { flex: 1 }]}>
										<Text style={orderStyle.lable}>备注</Text>
										<Text style={[orderStyle.value, orderStyle.lineFeed, { width: 250 * scale }]}>{remark || '-'}</Text>
									</View>
								</View>
							</View>
						</View>
					</View>

					<View style={styles.itemBox}>
						<View style={[styles.itemBody]}>

							{
								noticeItems && noticeItems.map((item, index) => {
									return (
										<View style={styles.bao} key={index}>
											<View style={[commonStyle.row2, styles.headPd]}>
												<Text style={styles.detailsHeadTitle}>采购批次:{item.procurementBatchNo || '-'}</Text>
											</View>
											<Text style={orderStyle.value}>{item.goodsName}</Text>
											<View style={[commonStyle.row3, { marginTop: 10 * scale }]}>
												<View style={[commonStyle.row3, { flex: 1, }]}>
													<Text style={commen.lable}>通知量 </Text>
													<Text style={orderStyle.value}>{item.noticeQuantity || '-'}{item.uomName}</Text>
												</View>
												<View style={[commonStyle.row3, { flex: 1 }]}>
													<Text style={[commen.lable]}>{orderType == 'P' ? '收货' : '发货'}量 </Text>
													<Text style={orderStyle.value}>{orderType == 'S' ? item.sendQuantity : item.signQuantity || '-'}{item.uomName}</Text>
												</View>
											</View>
										</View>
									)
								})
							}
						</View>
					</View>
				</ScrollView>
				<ModalCpt data={{
					isVisible,
					bodyData: [[
						{ title: '收货方', value: receivingWarehouseName || '-' },
						{ title: '收货地址', value: receivingAddress || '-' },
						{ title: '收货联系人', value: (shipper || '') + '-' + (shipperPhone || '') },
					], [
						{ title: '发货方', value: shipmentsWarehouseName || '-' },
						{ title: '发货地址', value: shipmentsAddress || '-' },
						{ title: '发货联系人', value: (consigner || '') + '-' + (consignerPhone || '') },
					]],
					modalCallBack: this._modalCallBack
				}} />
			</View >
		)
	}
}


const styles = StyleSheet.create({
	container: {
		flex: 1,
		backgroundColor: '#F5F5F5'
	},
	detailsHead: {
		//...commen.edge
	},
	detailsHeadTitle: {
		fontSize: 14 * scale,
		color: style.color.font1
	},
	detailsHeadText: {
		fontSize: 14 * scale,
		color: style.color.font1
	},
	detailsHeadStatus: {
		fontSize: 14 * scale,
		color: style.color.font1
	},
	topp: {
		marginBottom: 10 * scale
	},

	headPd: {
		paddingTop: 8 * scale,
		paddingBottom: 15 * scale,
	},
	itemBox: {
		...commen.edge,
		marginBottom: 20 * scale
	},
	itemBody: {
		backgroundColor: style.color.white,
		borderRadius: 4 * scale,
		paddingTop: 15 * scale,
		paddingBottom: 15 * scale,
		marginTop: 5 * scale,
		...commen.edge,

	},
	bao: {
		paddingBottom: 15 * scale,
		marginBottom: 10 * scale,
		borderBottomWidth: 1 / PixelRatio.get(),
		borderStyle: 'solid',
		borderBottomColor: '#E3E7F0',
	}
})